/**
 * Author: Todd Kerpelman
 * Date: 5/17/12
 * Time: 2:46 PM
 *
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
var someNames = ['Malcolm Washburne', 'Zoe Tam', 'Hoban Tam',
    'Inara Reynolds', 'Jayne Serra', 'Kaylee Cobb', 'Simon Washburne', 'River Washburne',
    'Derrial Frye', 'Saffron Briget'];

var someImages = [
    'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg',
  'https://lh6.googleusercontent.com/addafew/ofyourfriends/profiles/here.jpg'];

var users =[];

var getRandomInt = function(min, max) {
    return min + (Math.floor(Math.random() * (max-min +1)));

};

var bigRandomString = function() {
    var alphaNum = 'abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    var bigString = '';
    for( var i=0; i < 32; i++ ) {
        bigString += alphaNum.charAt(getRandomInt(0, alphaNum.length-1));
    }
    return bigString;
};

var createFakeUser = function() {
    var userInt = getRandomInt(0,9);
    var newUser = {
        person: {
          displayName: someNames[userInt],
          image: {url: someImages[userInt]}
        },
        id: bigRandomString()
    };
    return newUser;
};

var setupUsers = function() {
    var numberOfUsers = getRandomInt(1,5);
    for (var i=0; i<numberOfUsers; i++) {
        var newUser = createFakeUser();
        users.push(newUser);
    }
};

var flashIsReady = function() {
  getFolksInHangout();
  setTimeout(addNewUser, getRandomInt(5000, 10000));
  setTimeout(removeSomeUser, getRandomInt(5000, 10000));
  setInterval(generateVolumes, 300);

};

var getFolksInHangout = function() {
    console.log("Flash object is requesting folks in hangout")
    document.getElementById('HangoutInSpace')
        .tellFlashUsersInHangout(users);
};

var generateVolumes = function() {
    var volumeData = {};
    for (var i=0; i < users.length; i++) {
        volumeData[users[i].id] = getRandomInt(0,5);
    }
    document.getElementById('HangoutInSpace')
        .tellFlashNewVolumes({volumes: volumeData});
}

var addNewUser = function() {
  if (users.length < 10) {
      var nextUser = createFakeUser();
      users.push(nextUser);
      document.getElementById('HangoutInSpace')
          .tellFlashUserAdded(nextUser);
  }
  console.log("Adding a user!");
  setTimeout(addNewUser, getRandomInt(5000, 10000));

};

var removeSomeUser = function() {
    if (users.length > 1) {
        var userLeavingNum = getRandomInt(0, users.length-1);
        document.getElementById('HangoutInSpace')
            .tellFlashUserRemoved(users[userLeavingNum]);
        users.splice(userLeavingNum, 1);
    }
    console.log("Removing a user!");
    setTimeout(removeSomeUser, getRandomInt(5000, 10000));
}


var hangoutInit = function() {
  setupUsers();
  swfobject.embedSWF(
        "HangoutInSpace.swf", "flashContent",
        "640", "480",
        swfVersionStr, xiSwfUrlStr,
        flashvars, params, attributes);

};

hangoutInit();